﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
using UnityEngine;

// Token: 0x02000392 RID: 914
public class TagText
{
	// Token: 0x0600167A RID: 5754 RVA: 0x00096350 File Offset: 0x00094550
	public TagText()
	{
	}

	// Token: 0x0600167B RID: 5755 RVA: 0x00096364 File Offset: 0x00094564
	public TagText(string file)
	{
		this.Load_Text(file);
	}

	// Token: 0x0600167C RID: 5756 RVA: 0x00096380 File Offset: 0x00094580
	public TagText(TextAsset text)
	{
		this.Load_Data(text.text, text.name);
	}

	// Token: 0x0600167D RID: 5757 RVA: 0x000963A8 File Offset: 0x000945A8
	public TagText(string assetbundleDir, string assetbundleName, string name)
	{
		this.Load_AssetBundle(assetbundleDir, assetbundleName, name);
	}

	// Token: 0x17000234 RID: 564
	// (get) Token: 0x0600167E RID: 5758 RVA: 0x000963C4 File Offset: 0x000945C4
	public int ElementNum
	{
		get
		{
			return this.elements.Count;
		}
	}

	// Token: 0x17000235 RID: 565
	// (get) Token: 0x0600167F RID: 5759 RVA: 0x000963D4 File Offset: 0x000945D4
	public List<TagText.Element> Elements
	{
		get
		{
			return this.elements;
		}
	}

	// Token: 0x06001680 RID: 5760 RVA: 0x000963DC File Offset: 0x000945DC
	public void Load_Resource(string file)
	{
		TextAsset textAsset = Resources.Load(file) as TextAsset;
		if (textAsset == null)
		{
			GlobalData.Debug_LogWarning(file + "并没有打开");
			return;
		}
		this.Load_Data(textAsset.text, file);
	}

	// Token: 0x06001681 RID: 5761 RVA: 0x00096420 File Offset: 0x00094620
	public void Load_AssetBundle(string assetbundleDir, string assetbundleName, string name)
	{
		TextAsset textAsset = AssetBundleLoader.LoadAsset<TextAsset>(assetbundleDir, assetbundleName, name);
		if (textAsset == null)
		{
			GlobalData.Debug_LogWarning(name + "并没有打开");
			return;
		}
		string fileName = assetbundleName + "," + name;
		this.Load_Data(textAsset.text, fileName);
	}

	// Token: 0x06001682 RID: 5762 RVA: 0x00096470 File Offset: 0x00094670
	public void Load_TextAsset(TextAsset text)
	{
		this.Load_Data(text.text, text.name);
	}

	// Token: 0x06001683 RID: 5763 RVA: 0x00096484 File Offset: 0x00094684
	public void Load_Text(string file)
	{
		string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(file);
		FileStream fileStream = new FileStream(file, FileMode.Open, FileAccess.Read);
		StreamReader streamReader = new StreamReader(fileStream);
		this.Load_Data(streamReader.ReadToEnd(), fileNameWithoutExtension);
		streamReader.Close();
		fileStream.Close();
	}

	// Token: 0x06001684 RID: 5764 RVA: 0x000964C4 File Offset: 0x000946C4
	private void Load_Data(string txtStr, string fileName)
	{
		Regex regex = new Regex("\\[(?<tag>.+)\\]", RegexOptions.IgnoreCase | RegexOptions.Singleline);
		string[] array = new string[]
		{
			"\r\n"
		};
		string[] array2 = txtStr.Split(array, StringSplitOptions.None);
		for (int i = 0; i < array2.Length; i++)
		{
			string text = array2[i];
			if (!(text == string.Empty))
			{
				if (text.IndexOf("//") != 0)
				{
					Match match = regex.Match(text);
					if (match.Success)
					{
						string value = match.Groups["tag"].Value;
						TagText.Element element = new TagText.Element();
						i = element.Load(value, array2, i, fileName);
						this.elements.Add(element);
					}
				}
			}
		}
	}

	// Token: 0x06001685 RID: 5765 RVA: 0x0009658C File Offset: 0x0009478C
	public void AddElement(TagText.Element element)
	{
		this.elements.Add(element);
	}

	// Token: 0x06001686 RID: 5766 RVA: 0x0009659C File Offset: 0x0009479C
	public void Save(string file)
	{
		string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(file);
		FileStream fileStream = new FileStream(file, FileMode.Create, FileAccess.Write);
		StreamWriter streamWriter = new StreamWriter(fileStream, Encoding.UTF8);
		foreach (TagText.Element element in this.elements)
		{
			streamWriter.WriteLine("[" + element.Tag + "]");
			foreach (KeyValuePair<string, TagText.Attribute> keyValuePair in element.Attributes)
			{
				streamWriter.Write(keyValuePair.Key);
				streamWriter.Write(":");
				streamWriter.Write(keyValuePair.Value.valOriginal);
				streamWriter.WriteLine();
			}
			streamWriter.WriteLine();
		}
		streamWriter.Close();
		fileStream.Close();
	}

	// Token: 0x04001A47 RID: 6727
	private static readonly char[] separator = new char[]
	{
		':',
		','
	};

	// Token: 0x04001A48 RID: 6728
	private List<TagText.Element> elements = new List<TagText.Element>();

	// Token: 0x02000393 RID: 915
	public class Attribute
	{
		// Token: 0x06001688 RID: 5768 RVA: 0x000966CC File Offset: 0x000948CC
		public Attribute(string name, string valOriginal)
		{
			this.Setup(name, valOriginal);
		}

		// Token: 0x17000236 RID: 566
		// (get) Token: 0x06001689 RID: 5769 RVA: 0x000966E8 File Offset: 0x000948E8
		public int ValsNum
		{
			get
			{
				return this.vals.Count;
			}
		}

		// Token: 0x0600168A RID: 5770 RVA: 0x000966F8 File Offset: 0x000948F8
		public void Setup(string name, string valOriginal)
		{
			if (valOriginal == null)
			{
				return;
			}
			this.name = name;
			this.valOriginal = valOriginal;
			string[] array = valOriginal.Split(TagText.separator, StringSplitOptions.RemoveEmptyEntries);
			for (int i = 0; i < array.Length; i++)
			{
				this.vals.Add(array[i]);
			}
		}

		// Token: 0x0600168B RID: 5771 RVA: 0x0009674C File Offset: 0x0009494C
		public void Add(string valOriginal)
		{
			this.valOriginal = this.valOriginal + "\n" + valOriginal;
			string[] array = valOriginal.Split(TagText.separator, StringSplitOptions.RemoveEmptyEntries);
			for (int i = 0; i < array.Length; i++)
			{
				this.vals.Add(array[i]);
			}
		}

		// Token: 0x0600168C RID: 5772 RVA: 0x000967A0 File Offset: 0x000949A0
		public bool GetVal(ref string val, int id)
		{
			if (id >= 0 && id < this.vals.Count)
			{
				val = this.vals[id];
				return true;
			}
			return false;
		}

		// Token: 0x0600168D RID: 5773 RVA: 0x000967CC File Offset: 0x000949CC
		public bool GetVal(ref int val, int id)
		{
			if (id >= 0 && id < this.vals.Count)
			{
				try
				{
					val = int.Parse(this.vals[id]);
				}
				catch (Exception ex)
				{
					GlobalData.Debug_LogError(ex.Message + " " + this.vals[id]);
				}
				return true;
			}
			return false;
		}

		// Token: 0x0600168E RID: 5774 RVA: 0x00096844 File Offset: 0x00094A44
		public bool GetVal(ref float val, int id)
		{
			if (id >= 0 && id < this.vals.Count)
			{
				try
				{
					val = float.Parse(this.vals[id]);
				}
				catch (Exception ex)
				{
					GlobalData.Debug_LogError(ex.Message + " " + this.vals[id]);
				}
				return true;
			}
			return false;
		}

		// Token: 0x0600168F RID: 5775 RVA: 0x000968BC File Offset: 0x00094ABC
		public bool GetVal(ref bool val, int id)
		{
			if (id >= 0 && id < this.vals.Count)
			{
				try
				{
					val = bool.Parse(this.vals[id]);
				}
				catch (Exception ex)
				{
					GlobalData.Debug_LogError(ex.Message + " " + this.vals[id]);
				}
				return true;
			}
			return false;
		}

		// Token: 0x04001A49 RID: 6729
		public string name;

		// Token: 0x04001A4A RID: 6730
		public string valOriginal;

		// Token: 0x04001A4B RID: 6731
		public List<string> vals = new List<string>();
	}

	// Token: 0x02000394 RID: 916
	public class Element
	{
		// Token: 0x06001690 RID: 5776 RVA: 0x00096934 File Offset: 0x00094B34
		public Element()
		{
		}

		// Token: 0x06001691 RID: 5777 RVA: 0x00096948 File Offset: 0x00094B48
		public Element(string tag)
		{
			this.Tag = tag;
		}

		// Token: 0x17000237 RID: 567
		// (get) Token: 0x06001692 RID: 5778 RVA: 0x00096964 File Offset: 0x00094B64
		// (set) Token: 0x06001693 RID: 5779 RVA: 0x0009696C File Offset: 0x00094B6C
		public string Tag { get; private set; }

		// Token: 0x17000238 RID: 568
		// (get) Token: 0x06001694 RID: 5780 RVA: 0x00096978 File Offset: 0x00094B78
		public Dictionary<string, TagText.Attribute> Attributes
		{
			get
			{
				return this.attributes;
			}
		}

		// Token: 0x06001695 RID: 5781 RVA: 0x00096980 File Offset: 0x00094B80
		public int Load(string tag, string[] strs, int i, string fileName)
		{
			this.Tag = tag;
			for (i++; i < strs.Length; i++)
			{
				string text = strs[i];
				if (text == string.Empty)
				{
					return i;
				}
				int num = text.Length + 1;
				for (int j = 0; j < TagText.separator.Length; j++)
				{
					int num2 = text.IndexOf(TagText.separator[j]);
					if (num2 != -1)
					{
						num = Mathf.Min(num, num2);
					}
				}
				if (num >= text.Length)
				{
					GlobalData.Debug_LogWarning(string.Concat(new object[]
					{
						"不明な内容 ",
						fileName,
						" ",
						i,
						"行目 ",
						text
					}));
				}
				else
				{
					string name = text.Remove(num);
					string val = text.Remove(0, num + 1);
					this.AddAttribute(name, val);
				}
			}
			return i;
		}

		// Token: 0x06001696 RID: 5782 RVA: 0x00096A6C File Offset: 0x00094C6C
		public void AddAttribute(string name, string val)
		{
			TagText.Attribute attribute;
			if (this.attributes.ContainsKey(name))
			{
				attribute = this.attributes[name];
				attribute.Add(val);
			}
			else
			{
				attribute = new TagText.Attribute(name, val);
			}
			this.attributes[name] = attribute;
		}

		// Token: 0x06001697 RID: 5783 RVA: 0x00096ABC File Offset: 0x00094CBC
		public TagText.Attribute GetAttribute(string attribute)
		{
			if (this.attributes.ContainsKey(attribute))
			{
				return this.attributes[attribute];
			}
			return null;
		}

		// Token: 0x06001698 RID: 5784 RVA: 0x00096AE0 File Offset: 0x00094CE0
		public bool GetVal(ref string val, string attribute, int id)
		{
			return this.attributes.ContainsKey(attribute) && this.attributes[attribute].GetVal(ref val, id);
		}

		// Token: 0x06001699 RID: 5785 RVA: 0x00096B08 File Offset: 0x00094D08
		public bool GetVal(ref int val, string attribute, int id)
		{
			return this.attributes.ContainsKey(attribute) && this.attributes[attribute].GetVal(ref val, id);
		}

		// Token: 0x0600169A RID: 5786 RVA: 0x00096B30 File Offset: 0x00094D30
		public bool GetVal(ref float val, string attribute, int id)
		{
			return this.attributes.ContainsKey(attribute) && this.attributes[attribute].GetVal(ref val, id);
		}

		// Token: 0x0600169B RID: 5787 RVA: 0x00096B58 File Offset: 0x00094D58
		public bool GetVal(ref bool val, string attribute, int id)
		{
			return this.attributes.ContainsKey(attribute) && this.attributes[attribute].GetVal(ref val, id);
		}

		// Token: 0x04001A4C RID: 6732
		private Dictionary<string, TagText.Attribute> attributes = new Dictionary<string, TagText.Attribute>();
	}
}
